﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using MaciejGrabek.WP7.Services.Phone.Entities;
using System.IO;
using MaciejGrabek.WP7.Services.Phone.Serialization;
using System.Text;
using System.Collections.Generic;

namespace MaciejGrabek.WP7.Services.Phone.Tools
{
    public class GameServicesCaller
    {
        public string GameServicesUrl { get; set; }

        public string GameNormalizedName { get; set; }

        public string GameHash { get; set; }


        public GameServicesCaller(string url, string gameNormalizedName, string gameHash)
        {
            this.GameServicesUrl = url;
            this.GameNormalizedName = gameNormalizedName;
            this.GameHash = gameHash;
        }

        public void SaveGameResult(GameResultDto result, Action onDone)
        {
            result.GameHash = this.GameHash;
            WebRequest request = WebRequest.Create(String.Format("{0}/api/gameResults", GameServicesUrl));
            request.Method = "POST";
            request.ContentType = "application/json";
            string jsonRequestBody = JsonSerializationHelper.Serialize(result);
            request.BeginGetRequestStream(
                new AsyncCallback((asynchronousResult) =>
                                      {
                                          Stream postStream = request.EndGetRequestStream(asynchronousResult);
                                          byte[] byteArray = Encoding.UTF8.GetBytes(jsonRequestBody);
                                          postStream.Write(byteArray, 0, byteArray.Length);
                                          postStream.Close();

                                          // not working here...
                                          request.BeginGetResponse(
                                              new AsyncCallback(
                                                  asynchronousResultForGetResponse =>
                                                      {
                                                          onDone();
                                                      }), request);

                                      }), request);
        }

        public void GetGameResults(Action<IEnumerable<GameResultLightDto>> onSuccess, Action<Exception> onError )
        {
            WebClient webClient = new WebClient();
            webClient.OpenReadCompleted += delegate(object sender, OpenReadCompletedEventArgs e)
                                               {
                                                   if (e.Error != null)
                                                   {
                                                       onError(e.Error);
                                                       return;
                                                   }

                                                   List<GameResultLightDto> results =
                                                       JsonSerializationHelper.Deserialize<List<GameResultLightDto>>(
                                                           e.Result);
                                                   onSuccess(results);
                                               };
            webClient.OpenReadAsync(new Uri(String.Format("{0}/api/gameResults/{1}", GameServicesUrl, GameNormalizedName)));
        }
    }
}